package cn.sciento.farm.devicemanager.api.jsonrpc.impl;

import cn.sciento.farm.devicemanager.api.jsonrpc.DevInsectSituationLampJsonApi;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.common.util.DateUtil;
import cn.sciento.farm.devicemanager.common.util.ReturnUtil;
import cn.sciento.farm.devicemanager.dao.entity.DevCamera;
import cn.sciento.farm.devicemanager.dao.entity.DevInsectImage;
import cn.sciento.farm.devicemanager.dao.entity.DevInsectSituationLamp;
import cn.sciento.farm.devicemanager.dao.entity.DevInsectSituationLampHistory;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.gateway.downlink.service.MqttBlockedSendManager;
import cn.sciento.farm.devicemanager.gateway.downlink.service.impl.MqttSendService;
import cn.sciento.farm.devicemanager.gateway.model.downlink.InsectLampSwitchData;
import cn.sciento.farm.devicemanager.gateway.model.downlink.InsectLampConfigData;
import cn.sciento.farm.devicemanager.service.CameraService;
import cn.sciento.farm.devicemanager.service.DevInsectImageService;
import cn.sciento.farm.devicemanager.service.DevInsectSituationLampHistoryService;
import cn.sciento.farm.devicemanager.service.DevInsectSituationLampService;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
/**
 * @author Walton
 * @since 2020/8/24 9:32
 */
@Service
@AutoJsonRpcServiceImpl
public class DevInsectSituationLampJsonApiImpl implements DevInsectSituationLampJsonApi {

    @Autowired
    DevInsectSituationLampService devInsectLampService;

    @Autowired
    DevInsectSituationLampHistoryService insectLampHistoryService;

    @Autowired
    MqttBlockedSendManager mqttBlockedSendManager;

    @Autowired
    CameraService cameraService;

    @Autowired
    DevInsectImageService insectImageService;

    @Override
    public ResponseMessage insert(DevInsectSituationLamp insectLamp) {
        if(StringUtils.isEmpty(insectLamp.getName())
                || StringUtils.isEmpty(insectLamp.getEui())
                || StringUtils.isEmpty(insectLamp.getPhtotsSerial())
                || StringUtils.isEmpty(insectLamp.getValidateCode())
                || insectLamp.getAreaId() <= 0 || insectLamp.getDelay() <= 0){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        int count = devInsectLampService.insert(insectLamp);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS, count,insectLamp.getId());

    }

    @Override
    public ResponseMessage update(DevInsectSituationLamp insectLamp) {
        if (insectLamp.getId() <= 0) {
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        DevInsectSituationLamp insectSituationLamp = new DevInsectSituationLamp();
        insectSituationLamp.setId(insectLamp.getId());
        insectSituationLamp.setName(insectLamp.getName());
        insectSituationLamp.setRemark(insectLamp.getRemark());
        insectSituationLamp.setModbusAddr(insectLamp.getModbusAddr());
        insectSituationLamp.setPhtotsSerial(insectLamp.getPhtotsSerial());
        insectSituationLamp.setValidateCode(insectLamp.getValidateCode());
        insectSituationLamp.setDelay(insectLamp.getDelay());
        int count = devInsectLampService.update(insectSituationLamp);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS, insectSituationLamp, count);
    }

    @Override
    public ResponseMessage getById(Long id) {
        if (id < 1) {
            return ReturnUtil.getReturnMap(ResultConstant.INVALID_PARAMETER);
        }
        DevInsectSituationLamp insectSituationLamp = devInsectLampService.getById(id);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,insectSituationLamp);
    }

    @Override
    public ResponseMessage list(Integer areaId) {
        if (areaId < 1) {
            return ReturnUtil.getReturnMap(ResultConstant.INVALID_PARAMETER);
        }
        List<DevInsectSituationLamp> insectSituationLampList = devInsectLampService.getByAreaId(areaId);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,insectSituationLampList);
    }

    @Override
    public ResponseMessage deleteById(Long id) {
        int count = devInsectLampService.deleteById(id);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage controlSwitch(InsectLampSwitchData insectLampSwitchData) {
        if ( insectLampSwitchData.getId() == null
                || insectLampSwitchData.getModbusAddr() == null
                || insectLampSwitchData.getEui() == null) {
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        } else {
            DevInsectSituationLamp insectLamp = devInsectLampService.getById(insectLampSwitchData.getId());
            if (insectLamp == null) {
                return ReturnUtil.getReturnMap(ResultConstant.DEVICE_NOT_EXIST);
            }
            if (insectLamp.getOnline() == 0) {
                return ReturnUtil.getReturnMap(ResultConstant.DEVICE_OFFLINE);
            }
            int code = mqttBlockedSendManager.send(insectLampSwitchData).getCode();
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS, code);

        }
    }

    @Override
    public ResponseMessage getHistory(Integer addr, Long time) {
        if (addr < 1 || time < 1) {
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        Long beginTime = DateUtil.getTimeZeroHour(time).getTime();
        Long endTime = DateUtil.getTimeTomorrowZeroHour(time).getTime();
        List<DevInsectSituationLampHistory> historyList = insectLampHistoryService.getHistory(addr, beginTime, endTime);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,historyList);
    }


    @Override
    public ResponseMessage setPhtotsRatio(Long id, Integer phtotsRatio) {
        if (id < 1 || phtotsRatio < 0) {
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        DevInsectSituationLamp insectLamp = devInsectLampService.getById(id);
        if (insectLamp.getOnline() == 0) {
            return ReturnUtil.getReturnMap(ResultConstant.DEVICE_OFFLINE);
        }
        InsectLampConfigData devInsectLampConfigData = new InsectLampConfigData();
        devInsectLampConfigData.setEui(insectLamp.getEui());
        devInsectLampConfigData.setModbusAddr(insectLamp.getModbusAddr());
        devInsectLampConfigData.setRatio(phtotsRatio);
        Integer code = mqttBlockedSendManager.send(devInsectLampConfigData).getCode();
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,code);
    }

    @Override
    public ResponseMessage configInsectLamp(InsectLampConfigData insectLampConfigData) {
        if (insectLampConfigData.getId() == null
                || insectLampConfigData.getModbusAddr() ==null
                || insectLampConfigData.getEui() == null) {
            return ReturnUtil.getReturnMap(ResultConstant.INVALID_PARAMETER);
        } else {
            DevInsectSituationLamp insectLamp = devInsectLampService.getById(insectLampConfigData.getId());
            if (insectLamp == null) {
                return ReturnUtil.getReturnMap(ResultConstant.DEVICE_NOT_EXIST);
            }
            if (insectLamp.getOnline() == 0) {
                return ReturnUtil.getReturnMap(ResultConstant.DEVICE_OFFLINE);
            }
            int code = mqttBlockedSendManager.send(insectLampConfigData).getCode();
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS, code);
        }
    }

    // 状态刷新
    @Override
    public ResponseMessage statusRefresh(Long id) {
        if (id < 1) {
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        DevInsectSituationLamp insectLamp = devInsectLampService.getById(id);
        if (insectLamp.getOnline() == 0) {
            return ReturnUtil.getReturnMap(ResultConstant.DEVICE_OFFLINE);
        }
        InsectLampSwitchData insectLampControlData = new InsectLampSwitchData();
        insectLampControlData.setEui(insectLamp.getEui());
        insectLampControlData.setModbusAddr(insectLamp.getModbusAddr());
        insectLampControlData.setStatusRefresh(1);
        Integer code = mqttBlockedSendManager.send(insectLampControlData).getCode();
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,code);
    }

    // 参数刷新
    @Override
    public ResponseMessage paramRefresh(Long id) {
        if (id < 1) {
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        DevInsectSituationLamp insectLamp = devInsectLampService.getById(id);
        if (insectLamp.getOnline() == 0) {
            return ReturnUtil.getReturnMap(ResultConstant.DEVICE_OFFLINE);
        }
        InsectLampConfigData devInsectLampConfigData = new InsectLampConfigData();
        devInsectLampConfigData.setEui(insectLamp.getEui());
        devInsectLampConfigData.setModbusAddr(insectLamp.getModbusAddr());
        devInsectLampConfigData.setParamRefresh(1);
        Integer code = mqttBlockedSendManager.send(devInsectLampConfigData).getCode();
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,code);
    }

    @Override
    public ResponseMessage getPhtotsImages(Long insectId, Long time) {
        if (insectId < 1) {
            return ReturnUtil.getReturnMap(ResultConstant.INVALID_PARAMETER);
        }
        Long beginTime = DateUtil.getTimeZeroHour(time).getTime();
        Long endTime = DateUtil.getTimeTomorrowZeroHour(time).getTime();
        List<DevInsectImage> phtotsImages = insectImageService.getPhtotsImages(insectId, beginTime, endTime);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,phtotsImages);
    }

    @Override
    public ResponseMessage discernPhtotsImage(String imageUrl) {
        if (StringUtils.isEmpty(imageUrl)) {
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        //TODO 调用害虫识别api
        return null;
    }

}
